package com.xyd.Servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xyd.Service.CommentService;
import com.xyd.bean.Comment;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@WebServlet({"/admin/comment","/admin/commentAjax","/admin/addComment","/admin/commentDetail","/admin/commentModify","/admin/commentDel"})
public class AdminManageCommentServlet extends HttpServlet {
    CommentService commentService = new CommentService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletPath();
        if ("/admin/addComment".equals(path)){
            doAddComment(request,response);
        }else if("/admin/commentAjax".equals(path)){
            doGetAllAjax(request,response);
        }else if ("/admin/comment".equals(path)){
            doGetAll(request,response);
        }else if ("/admin/commentDetail".equals(path)){
            doCommentDetail(request,response);
        }else if ("/admin/commentModify".equals(path)){
            doModifyComment(request,response);
        }else if ("/admin/commentDel".equals(path)){
            doDelComment(request,response);
        }
    }

    private void doDelComment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        int cid= Integer.parseInt(id);
        int i = commentService.delComment(cid);
        if (i >0){
            response.sendRedirect("/admin/comment");
        }
    }

    private void doModifyComment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        int cid= Integer.parseInt(id);
        String reply = request.getParameter("reply");
        Comment comment =new Comment();
        comment.setId(cid);
        comment.setReply(reply);
        int i = commentService.modifyComment(comment);
        if (i >0){
            response.sendRedirect("/admin/comment");
        }
    }

    private void doCommentDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        Comment comment = commentService.getByid(Integer.parseInt(id));
        if (comment != null){
            request.setAttribute("comment",comment);
            request.getRequestDispatcher("/manage/guestbook-modify.jsp").forward(request,response);
        }
    }

    private void doGetAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        List<Comment> comments = commentService.getAll();
        if (comments.size()>0){
            request.getSession().setAttribute("comments",comments);
            request.getRequestDispatcher("/manage/guestbook.jsp").forward(request,response);
        }else {
            response.sendRedirect("/manage/guestbook.jsp");
        }
    }
    private void doGetAllAjax(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        List<Comment> comments = commentService.getAll();
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(comments);
        PrintWriter out = response.getWriter();
        out.println(json);
    }

    private void doAddComment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String nickname = request.getParameter("nickname");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        if (nickname != null && title != null && content != null){
            Comment comment = new Comment(title,content,nickname);
            int i = commentService.addComment(comment);
            if (i >0){
                response.getWriter().println("true");
            }
        }
    }
}
